package bb.lanxing.nav;

import java.lang.ref.WeakReference;

import bb.lanxing.activity.sport.SportActivity;
import bb.lanxing.model.json.RouteStep;

public class NavInfoCallback extends INavigationServiceCallback.Stub {
    private WeakReference<SportActivity> reference;

    public NavInfoCallback(SportActivity sportActivity) {
        reference = new WeakReference<>(sportActivity);
    }

    @Override
    public void onNavStart() {
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> {
//                    sportActivity.chooseNavEnd(4);
            });
        }
    }

    @Override
    public void onNavUpdate(final int i, final String str, final double d) {
        System.out.println("onNavUpdate");
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> sportActivity.updateNavInfo(i, str, d));
        }
    }

    @Override
    public void onNavYaw(RouteStep routeStep) {
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> sportActivity.updateYaw(true));
        }
    }

    @Override
    public void onYawBack(RouteStep routeStep) {
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> sportActivity.updateYaw(false));
        }
    }

    @Override
    public void onArrived() {
        NavServiceManager.getInstance().unbindService();
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> {
//                    sportActivity.chooseNavEnd(1);
            });
        }
    }

    @Override
    public void onNavFailed(final int i) {
        final SportActivity sportActivity = reference.get();
        if (sportActivity != null) {
            sportActivity.handler.post(() -> sportActivity.onNavigationFailed(i));
        }
    }
}